/*
 * Copyright (c) 2017.
 * @author fff
 * 17-10-23 下午5:07
 *
 */

package com.cpit.qualitycontrol.web.controller;

import com.cpit.platform.common.constant.LogConstant;
import com.cpit.platform.common.entity.Result;
import com.cpit.platform.common.entity.TableDataQueryCondition;
import com.cpit.platform.common.entity.TableJsonData;
import com.cpit.platform.dto.OperLog;
import com.cpit.platform.log.LogOperator;
import com.cpit.platform.service.OperLogService;
import com.cpit.qualitycontrol.common.constant.LogConstantEx;
import com.cpit.qualitycontrol.entity.Cooperator;
import com.cpit.qualitycontrol.service.CooperatorService;
import com.cpit.qualitycontrol.service.impl.ServiceUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/Cooperator")
public class CooperatorController {private final String loggerName = this.getClass().getName();
    private String logModule= "合作方：";
    @Autowired
    private CooperatorService cooperatorService = null;
    @Autowired
    private OperLogService operLogService;

    @ApiOperation(value="查询合作方", notes="通过ID查询合作方详细信息")
    //@PreAuthorize("hasAuthority('queryCooperator')")
    @RequestMapping(value="/queryCooperator/{id}", method = RequestMethod.GET)
    public Result<Cooperator> queryCooperator(@ApiParam(value = "合作方ID", required = true) @PathVariable(value = "id") Long id) {
        return new Result<>(Result.CODE_SUCCESS, "", this.cooperatorService.findById(id));
    }

    @ApiOperation(value="查询所有合作方", notes="无条件查询合作方信息，返回对象List")
    //@PreAuthorize("hasAuthority('queryCooperator')")
    @RequestMapping(value="/queryAllCooperator", method = RequestMethod.GET)
    public List<Cooperator> queryAllCooperator() {
        return this.cooperatorService.findAll();
    }

    @ApiOperation(value="查询合作方列表", notes="通过条件查询合作方信息，返回List方式" +
            "支持模糊查询。")
    //@PreAuthorize("hasAuthority('queryCooperator')")
    @RequestMapping(value="/queryCooperator", method = RequestMethod.GET)
    public List<Cooperator> queryCooperator(
            @ApiParam(value = "当前页数， 以1开始。") @RequestParam(value = "page", required = false, defaultValue = "1") int page,
            @ApiParam(value = "当前页的内容长度。") @RequestParam(value = "size", required = false, defaultValue = "20") int size,
            @ApiParam(value = "排序方式：asc为升序，desc为降序。") @RequestParam(value = "order", required = false, defaultValue = "asc") String order,
            @ApiParam(value = "排序字段，与dto中声明的字段名称一致。") @RequestParam(value = "orderBy", required = false, defaultValue = "id") String orderBy,
            @ApiParam(value = "模糊搜索的字符串，根据角色的名称字段进行模糊匹配。") @RequestParam(value = "pattern", required = false, defaultValue = "") String pattern) {
        return this.cooperatorService.findByCondition(page, size, order, orderBy, pattern);
    }

    @ApiOperation(value="分页查询合作方", notes="通过条件查询合作方信息，返回jqGrid分页JSON形式，支持模糊查询。")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cond", value = "JqGrid的条件对象，由JqGrid根据js参数自动封装。", required = true, dataType = "TableDataQueryCondition")
    })
    @SuppressWarnings("rawtypes")
    //@PreAuthorize("hasAuthority('queryCooperator')")
    @RequestMapping(value="/queryCooperatorJqg", method = RequestMethod.GET)
    public TableJsonData queryCooperatorJqg(TableDataQueryCondition cond) {
        return this.cooperatorService.findByConditionJqg(cond);
    }

    @ApiOperation(value="创建合作方", notes="用户手工指定合作方并创建，" +
            "需要手动输入合作方名称和描述信息，合作方名称不能重复。")
    @ApiImplicitParam(name = "data", value = "合作方对象", required = true, dataType = "Cooperator")
    //@PreAuthorize("hasAuthority('createCooperator')")
    @RequestMapping(value="/createCooperator", method = RequestMethod.POST)
    public Result<Cooperator> createCooperator(@RequestBody Cooperator data) {
        try{
            Cooperator retCooperator = this.cooperatorService.create(data);
            writeLog(LogConstant.OPER_CREATE, Result.CODE_SUCCESS,logModule+retCooperator.getName());
            return new Result<>(Result.CODE_SUCCESS, "", retCooperator);
        } catch(Exception e) {
            LogOperator.err(loggerName, "createCooperator error:" + data, e);
            writeLog(LogConstant.OPER_CREATE, Result.CODE_FAILED,logModule+e.getMessage());
            return new Result<>(Result.CODE_FAILED, e.getMessage(), data);
        }
    }

    @ApiOperation(value="修改合作方", notes="用户手工指定合作方并修改，" +
            "需要手动输入合作方名称和描述信息，合作方名称不能重复。")
    @ApiImplicitParam(name = "data", value = "合作方对象", required = true, dataType = "Cooperator")
    //@PreAuthorize("hasAuthority('modifyCooperator')")
    @RequestMapping(value="/modifyCooperator", method = RequestMethod.POST)
    public Result<Cooperator> modifyCooperator(@RequestBody Cooperator data) {
        try{
            Cooperator retCooperator = this.cooperatorService.modify(data);
            writeLog(LogConstant.OPER_MODIFY, Result.CODE_SUCCESS,logModule+retCooperator.getName());
            return new Result<>(Result.CODE_SUCCESS, "", retCooperator);
        } catch(Exception e) {
            LogOperator.err(loggerName, "modifyCooperator error:" + data, e);
            writeLog(LogConstant.OPER_MODIFY, Result.CODE_FAILED,logModule+e.getMessage());
            return new Result<>(Result.CODE_FAILED, e.getMessage(), data);
        }
    }

    @ApiOperation(value="删除合作方", notes="在系统中删除指定合作方")
    @ApiImplicitParam(name = "data", value = "合作方对象", required = true, dataType = "Cooperator")
    //@PreAuthorize("hasAuthority('deleteCooperator')")
    @RequestMapping(value="/deleteCooperator", method = RequestMethod.DELETE)
    public Result<Cooperator> deleteCooperator(@RequestBody Cooperator data) {
        try{
            this.cooperatorService.delete(data);
            writeLog(LogConstant.OPER_DELETE, Result.CODE_SUCCESS,logModule+data.getName());
            return new Result<>(Result.CODE_SUCCESS, "", data);
        } catch(Exception e) {
            LogOperator.err(loggerName, "deleteCooperator error:" + data, e);
            writeLog(LogConstant.OPER_DELETE, Result.CODE_FAILED,logModule+e.getMessage());
            return new Result<>(Result.CODE_FAILED, e.getMessage(), data);
        }
    }

    @ApiOperation(value="删除合作方", notes="在系统中删除指定合作方")
    //@PreAuthorize("hasAuthority('deleteCooperator')")
    @RequestMapping(value="/deleteCooperator/{id}", method = RequestMethod.DELETE)
    public Result<Cooperator> deleteCooperator(@ApiParam(value = "合作方ID", required = true) @PathVariable("id") Long id) {
        try{
            Cooperator cooperator = cooperatorService.findById(id);
            this.cooperatorService.delete(id);
            writeLog(LogConstant.OPER_DELETE, Result.CODE_SUCCESS,logModule+cooperator.getName());
            return new Result<>(Result.CODE_SUCCESS, id.toString());
        } catch(Exception e) {
            LogOperator.err(loggerName, "deleteCooperator error:id=" + id, e);
            writeLog(LogConstant.OPER_DELETE, Result.CODE_FAILED,logModule+e.getMessage());
            return new Result<>(Result.CODE_FAILED, e.getMessage());
        }
    }
    private void writeLog(String op,String result, String detail){
        OperLog operLog =new OperLog();
        operLog.setOperateTime(new Date());
        operLog.setModule(LogConstantEx.MODULE_COOPERATOR);
        operLog.setCommand(op);
        operLog.setResult(result);
        operLog.setOperateUser(ServiceUtils.getLoginUser().getUsername());
        operLog.setDetail(detail);
        operLogService.writeLog(operLog);
    }
    
}
